package com.doge.controller.admin;

import com.doge.bean.base.BaseRespVo;
import com.doge.bean.pojo.system.System;
import com.doge.exception.Param402Exception;
import com.doge.service.admin.config.ConfigService;
import com.doge.util.valid.ParamValidation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: ConfigController
 * @Description:
 * @author: PuJing
 * @create: 2021-11-30-20:48
 **/
@RestController
@RequestMapping("admin/config")
public class ConfigController {

    @Autowired
    ConfigService configService;

    /**
     * 获得商城配置
     *
     * @return
     */
    /*@RequestMapping("mall")
    public BaseRespVo mall(){
    }*/
    @RequestMapping(value = "order", method = RequestMethod.GET)
    public BaseRespVo orderGet() {

        HashMap<String, String> orderResult = new HashMap<>();

        orderResult = configService.getOrderConfig();

        return BaseRespVo.ok(orderResult);
    }

    @RequestMapping(value = "order", method = RequestMethod.POST)
    public BaseRespVo orderPost(@RequestBody Map map) throws Param402Exception {

        // 必须为数字，否则报错
        /*market_order_unpaid         用户下单后超时、用户未付款，则订单自动取消
        market_order_unconfirm   订单发货后超期，未确认收货，则订单自动确认收货
        market_order_comment*/

        try {
            Integer market_order_unpaid = Integer.parseInt((String) map.get("market_order_unpaid"));
            Integer market_order_unconfirm = Integer.parseInt((String) map.get("market_order_unconfirm"));
            Integer market_order_comment = Integer.parseInt((String) map.get("market_order_comment"));
        } catch (Exception e) {
            throw new Param402Exception("参数值不对");
        }

        try {
            Integer market_order_unpaid = Integer.parseInt((String) map.get("market_order_unpaid"));
            Integer market_order_unconfirm = Integer.parseInt((String) map.get("market_order_unconfirm"));
            Integer market_order_comment = Integer.parseInt((String) map.get("market_order_comment"));

            if (
                    market_order_unpaid <= 0 || market_order_unconfirm <= 0 || market_order_comment <= 0) {
                throw new Exception();
            }

        } catch (Exception e) {
            throw new Param402Exception("参数值必须大于0");
        }

        int code = configService.postConfig(map);

        if (code == 200) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.error(621, "修改失败");
        }
    }


    @RequestMapping(value = "mall", method = RequestMethod.GET)
    public BaseRespVo mallGet() {

        HashMap<String, String> mallResult;

        mallResult = configService.getMallConfig();

        return BaseRespVo.ok(mallResult);
    }

    @RequestMapping(value = "mall", method = RequestMethod.POST)
    public BaseRespVo mallPost(@RequestBody Map map) throws Param402Exception {

        //market_mall_longitude   经度  market_mall_latitude  维度
        //market_mall_phone 联系电话
        //market_mall_qq  联系qq

        try {

            Double market_mall_longitude = Double.parseDouble((String) map.get("market_mall_longitude"));
            Double market_mall_latitude = Double.parseDouble((String) map.get("market_mall_latitude"));
            Long market_mall_phone = Long.parseLong((String) map.get("market_mall_phone"));
            Integer market_mall_qq = Integer.parseInt((String) map.get("market_mall_qq"));

        } catch (Exception e) {
            throw new Param402Exception("参数值不对");
        }


        try {
            Double market_mall_longitude = Double.parseDouble((String) map.get("market_mall_longitude"));
            Double market_mall_latitude = Double.parseDouble((String) map.get("market_mall_latitude"));
            Long market_mall_phone = Long.parseLong((String) map.get("market_mall_phone"));
            Integer market_mall_qq = Integer.parseInt((String) map.get("market_mall_qq"));

            if (
                    market_mall_longitude <= 0 || market_mall_latitude <= 0 || market_mall_phone <= 0 || market_mall_qq<=0) {
                throw new Exception();
            }

        } catch (Exception e) {
            throw new Param402Exception("参数值必须大于0");
        }



        String market_mall_phone1 = (String) map.get("market_mall_phone");

        if (!ParamValidation.phoneNumValId(market_mall_phone1)) {

            throw new Param402Exception("号码格式是以“135、155、177、139、153、187为开头的11位数字”");
        }

        int code = configService.postConfig(map);

        if (code == 200) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.error(621, "修改失败");
        }
    }


    @RequestMapping(value = "express", method = RequestMethod.GET)
    public BaseRespVo expressGet() {

        HashMap<String, String> expressResult;

        expressResult = configService.getExpressConfig();

        return BaseRespVo.ok(expressResult);
    }

    @RequestMapping(value = "express", method = RequestMethod.POST)
    public BaseRespVo expressPost(@RequestBody Map map) throws Param402Exception {

        /*market_express_freight_min  运费满减所需最低消费
        market_express_freight_value  运费满减不足所需运费*/
        try {
            Integer market_express_freight_min = Integer.parseInt((String) map.get("market_express_freight_min"));
            Integer market_express_freight_value = Integer.parseInt((String) map.get("market_express_freight_value"));

        } catch (Exception e) {
            throw new Param402Exception("参数值不对");
        }

        try {
            Integer market_express_freight_min = Integer.parseInt((String) map.get("market_express_freight_min"));
            Integer market_express_freight_value = Integer.parseInt((String) map.get("market_express_freight_value"));


            if (
                    market_express_freight_min <= 0 || market_express_freight_value <= 0 ) {
                throw new Exception();
            }

        } catch (Exception e) {
            throw new Param402Exception("参数值必须大于0");
        }


        int code = configService.postConfig(map);

        if (code == 200) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.error(621, "修改失败");
        }
    }

    @RequestMapping(value = "wx", method = RequestMethod.GET)
    public BaseRespVo wxGet() {

        HashMap<String, String> wxResult;

        wxResult = configService.getWxConfig();

        return BaseRespVo.ok(wxResult);
    }

    @RequestMapping(value = "wx", method = RequestMethod.POST)
    public BaseRespVo wxPost(@RequestBody Map map) throws Param402Exception {

        /*market_wx_index_new  新品首发栏商品显示数量
market_wx_index_hot  人气推荐栏目商品显示数量
market_wx_index_brand    品牌制造商直供栏目品牌商显示数量
market_wx_index_topic     专题精选栏目显示数量
market_wx_catlog_list   分类栏目显示数量
market_wx_catlog_goods  分类栏目商品显示数量
market_wx_share  商品分享功能*/

        try {

            Integer market_wx_index_new = Integer.parseInt((String) map.get("market_wx_index_new"));
            Integer market_wx_index_hot = Integer.parseInt((String) map.get("market_wx_index_hot"));
            Integer market_wx_index_brand = Integer.parseInt((String) map.get("market_wx_index_brand"));
            Integer market_wx_index_topic = Integer.parseInt((String) map.get("market_wx_index_topic"));
            Integer market_wx_catlog_list = Integer.parseInt((String) map.get("market_wx_catlog_list"));
            Integer market_wx_catlog_goods = Integer.parseInt((String) map.get("market_wx_catlog_goods"));

        } catch (Exception e) {
            throw new Param402Exception("参数值不对");
        }


        try {
            Integer market_wx_index_new = Integer.parseInt((String) map.get("market_wx_index_new"));
            Integer market_wx_index_hot = Integer.parseInt((String) map.get("market_wx_index_hot"));
            Integer market_wx_index_brand = Integer.parseInt((String) map.get("market_wx_index_brand"));
            Integer market_wx_index_topic = Integer.parseInt((String) map.get("market_wx_index_topic"));
            Integer market_wx_catlog_list = Integer.parseInt((String) map.get("market_wx_catlog_list"));
            Integer market_wx_catlog_goods = Integer.parseInt((String) map.get("market_wx_catlog_goods"));

            if (
                    market_wx_index_new <= 0 || market_wx_index_hot <= 0 || market_wx_index_brand <= 0 || market_wx_index_topic <= 0 || market_wx_catlog_list <= 0 || market_wx_catlog_goods <= 0  ) {
                throw new Exception();
            }

        } catch (Exception e) {
            throw new Param402Exception("参数值必须大于0");
        }

        int code = configService.postConfig(map);

        if (code == 200) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.error(621, "修改失败");
        }
    }


}
